﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace myTwitter.BO
{
    public class Trends
    {
        public ObservableCollection<TrendTopic> CurrentTrends { get; set; }

        public Trends()
        {
        }

        public Trends(string rawTrends)
        {
            CurrentTrends = new ObservableCollection<TrendTopic>();
            if (AreValidTrends(rawTrends))
            {
                /*rawTrends = rawTrends.Replace(@"{""trends"":[",string.Empty);
                int endIndex = rawTrends.LastIndexOf(@"],""as_of"":");*/
                rawTrends = rawTrends.Substring(rawTrends.IndexOf("[") + 1, rawTrends.IndexOf("]") - (rawTrends.IndexOf("[") + 1));
                string[] trends = rawTrends.Split(',');
                for (int i = 0; i <= trends.Length; i++)
                {
                    if (i > 0 && i % 2 == 0)
                    {
                        string fixedTrend = string.Format("{0},{1}", trends[i - 2], trends[i - 1]);
                        CurrentTrends.Add(new TrendTopic(fixedTrend));
                    }
                }
            }
        }

        private bool AreValidTrends(string rawTrends)
        {
            if (!string.IsNullOrEmpty(rawTrends))
            {
                if (rawTrends.IndexOf("[")>0 && rawTrends.IndexOf("]")>0 && rawTrends.IndexOf("as_of")>0)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
